package com.leo.manage_system.base.exceptionhandler;

/**
 * @Author: ywzjks
 * @Date: 2021/11/4 14:02
 * @Description:
 */
import com.leo.manage_system.utils.*;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/** * 统一异常处理类 */

@ControllerAdvice
public class GlobalExceptionHandler {

    //指定出现什么异常执行这个方法
    @ExceptionHandler(Exception.class)
    @ResponseBody //为了返回数据
    public R error(Exception e){
        e.printStackTrace();
        return R.error().message("执行了全局异常处理..");

    }

    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public R error(ArithmeticException e){
        e.printStackTrace();
        return R.error().message("执行了算术异常");
    }
    //自定义异常
    /**
     * 用法
     * try {
     *     int a = 10/0;
     * }
     * catch(Exception e) {
     * throw new  GuliException(20001,"出现自定义异常");
     * }
     * */
    @ExceptionHandler(GuliException.class)
    @ResponseBody
    public R error(GuliException e)
    {    e.printStackTrace();
        return R.error().message(e.getMsg()).code(e.getCode());
    }
}



